<template>
	<view class="wrap">
		<u-toast ref="uToast" />
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录汇鸽</view>
			<view class="loginTips">首次登录请先绑定账号</view>
			<u-form :model="form" ref="uForm">
				<u-form-item label="手机号" prop="phone">
					<u-input v-model="form.phone" />
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u-input v-model="form.password" type="password" />
				</u-form-item>
			</u-form>
			<button @click="submit" :style="[inputStyle]" class="bindButton">绑定账号</button>
			<view class="alternative">
				<view class="issue">遇到问题</view>
			</view>
		</view>
	</view>
</template>

<script>
import { bindWx } from '@/services/user/index.js'
export default {
	data() {
		return {
			form: {
				phone: "",
				password: "",
				account: "",
				code: ""
			},
			rules: {
				phone: [{
					required: true,
					message: '请输入手机号',
					// 可以单个或者同时写两个触发验证方式 
					trigger: ['change', 'blur'],
				}],
				password: [{
					required: true,
					message: '请输入密码',
					trigger: ['change', 'blur'],
				}]
			},
		}
	},
	computed: {
		inputStyle() {
			let style = {};
			if(this.tel) {
				style.color = "#fff";
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onLoad(option) {
		this.form.account = option.account;
	},
	methods: {
		submit() {
			this.$refs.uForm.validate(async valid => {
				if (valid) {
					uni.login({
						provider: 'weixin',
						success: async (loginRes) => {
							console.log(loginRes)
							this.form.code = loginRes.code;
							await bindWx(this.form).then(res => {
								console.log(res)
								if(res.data.statusCode === 20000) {
									this.$refs.uToast.show({
										title: '绑定成功！',
										type: 'success',
										duration: '2300'
									})
									uni.reLaunch({
										url: '/pages/user/index?showFlag=' + true,
										animationType: 'pop-in',
										animationDuration: 200
									})
								}
							})
						}
					});
					
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 50rpx;
		}
		.loginTips {
			text-align: left;
			font-size: 40rpx;
			font-weight: 500;
			margin-bottom: 50rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.bindButton {
			background-color: rgb(0, 253, 182);
			border: none;
			color: #fff;
			font-weight: 700;
			font-size: 30rpx;
			padding: 12rpx 0;
			
			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
}
</style>

